package com.tfswx.jcyw.dmzc.util;

import cn.hutool.core.io.FileUtil;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.WinUser;
import lombok.SneakyThrows;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public final class ImageUtil {


    private ImageUtil() {
    }


    public static ImageIcon getImageIconByFileName(String fileName) {
        URL resource1 = ImageUtil.class.getClassLoader().getResource("images/" + fileName);
        return new ImageIcon(resource1);
    }


    @SneakyThrows
    public static byte[] getWindowImage() {
        WinDef.HWND hwnd = User32.INSTANCE.GetForegroundWindow();
        WinUser.WINDOWINFO windowinfo = new WinUser.WINDOWINFO();
        User32.INSTANCE.GetWindowInfo(hwnd, windowinfo);
        Rectangle rectangle = windowinfo.rcClient.toRectangle();
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice defaultScreenDevice = ge.getDefaultScreenDevice();
        GraphicsConfiguration configuration = defaultScreenDevice.getDefaultConfiguration();
        AffineTransform defaultTransform = configuration.getDefaultTransform();
        double scaleX = defaultTransform.getScaleX();
        double scaleY = defaultTransform.getScaleY();
        Rectangle re = new Rectangle((int) (rectangle.getX() / scaleX), (int) (rectangle.getY() / scaleY), (int) (rectangle.getWidth() / scaleX), (int) (rectangle.getHeight() / scaleY));
        BufferedImage bufferedImage = (new Robot()).createScreenCapture(re);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageIO.write(bufferedImage, "png", os);
        return os.toByteArray();
    }


    @SneakyThrows
    public static byte[] getScreenImage() {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        double minX = Double.MAX_VALUE;
        double minY = Double.MAX_VALUE;
        double maxX = Double.MIN_VALUE;
        double maxY = Double.MIN_VALUE;
        List<Map<String, Object>> screenList = new ArrayList<>();
        Map<String, Object> map = null;
        GraphicsDevice[] screenDevices = ge.getScreenDevices();

        Rectangle size;
        for (GraphicsDevice screenDevice : screenDevices) {
            GraphicsConfiguration configuration = screenDevice.getDefaultConfiguration();
            size = configuration.getBounds();
            double x1 = size.getX();
            double y1 = size.getY();
            minX = Math.min(minX, x1);
            minY = Math.min(minY, y1);
            maxX = Math.max(maxX, x1 + size.getWidth());
            maxY = Math.max(maxY, y1 + size.getHeight());
            BufferedImage screenCapture = (new Robot()).createScreenCapture(size);
            map = new HashMap<>();
            map.put("img", screenCapture);
            map.put("size", size);
            screenList.add(map);
        }

        BufferedImage imageNew = new BufferedImage((int) (maxX - minX), (int) (maxY - minY), 1);

        for (Map<String, Object> objectMap : screenList) {
            BufferedImage img = (BufferedImage) objectMap.get("img");
            size = (Rectangle) objectMap.get("size");
            int width = img.getWidth();
            int height = img.getHeight();
            int[] rgb = img.getRGB(0, 0, width, height, null, 0, width);
            imageNew.setRGB((int) (size.getX() - minX), (int) (size.getY() - minY), width, height, rgb, 0, width);
        }

        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageIO.write(imageNew, "png", os);
        return os.toByteArray();
    }


    public static Rectangle getScreenSize() {
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        double minX = Double.MAX_VALUE;
        double minY = Double.MAX_VALUE;
        double maxX = Double.MIN_VALUE;
        double maxY = Double.MIN_VALUE;
        GraphicsDevice[] screenDevices = ge.getScreenDevices();

        for (GraphicsDevice screenDevice : screenDevices) {
            GraphicsConfiguration configuration = screenDevice.getDefaultConfiguration();
            Rectangle bounds = configuration.getBounds();
            minX = Math.min(minX, bounds.getX());
            minY = Math.min(minY, bounds.getY());
            maxX = Math.max(maxX, bounds.getX() + bounds.getWidth());
            maxY = Math.max(maxY, bounds.getY() + bounds.getHeight());
        }

        return new Rectangle((int) minX, (int) minY, (int) (maxX - minX), (int) (maxY - minY));
    }


    public static void main(String[] args) {
        byte[] screenImage = getScreenImage();
        File file = new File("E:\\workspace\\idea-plugin\\dmzc\\截图1.png");
        FileUtil.writeBytes(screenImage, file);
    }
}
